﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.UI.HtmlControls;

namespace GUI.OpenID
{
    public class Util
    {
        protected const string OAuthParameterPrefix = "oauth_";

        public static string GetAppSettingValue(string key)
        {
            string value = "";
            value = ConfigurationManager.AppSettings.Get(key);
            return value;
        }

        public static string GetAuthenUrl()
        {
            return String.Format(GetAppSettingValue("mingAuthenUrl"), GetAppSettingValue("mingConsumerKey"), GetAppSettingValue("mingConsumerSecret"), GetAppSettingValue("site_callback"));
        }
        public static string getData(string url)
        {
            try
            {
                string result = string.Empty;

                WebRequest wrq = WebRequest.Create(url);
                WebResponse wrs = wrq.GetResponse();

                StreamReader strdr = new StreamReader(wrs.GetResponseStream(), Encoding.UTF8);
                result = strdr.ReadToEnd();
                strdr.Close();
                wrs.Close();
                return result;

            }
            catch { return ""; }
        }
        public static List<GUI.OpenID.OAuthBase.QueryParameter> GetQueryParameters(string parameters)
        {
            if (parameters.StartsWith("?"))
            {
                parameters = parameters.Remove(0, 1);
            }

            List<GUI.OpenID.OAuthBase.QueryParameter> result = new List<GUI.OpenID.OAuthBase.QueryParameter>();

            if (!string.IsNullOrEmpty(parameters))
            {
                string[] p = parameters.Split('&');
                foreach (string s in p)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        if (s.IndexOf('=') > -1)
                        {
                            string[] temp = s.Split('=');
                            result.Add(new GUI.OpenID.OAuthBase.QueryParameter(temp[0], temp[1]));
                        }
                        else
                        {
                            result.Add(new GUI.OpenID.OAuthBase.QueryParameter(s, string.Empty));
                        }
                    }
                }
            }

            return result;
        }
        public static string GetUserName()
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                FormsIdentity ident = HttpContext.Current.User.Identity as FormsIdentity;

                if (ident != null)
                {

                    FormsAuthenticationTicket ticket = ident.Ticket;

                    string[] userData = ticket.UserData.Split('|');

                    if (userData.Length >= 3)
                    {
                        return userData[0];
                    }
                    else
                    {
                        return "Undefine";
                    }

                }
                else
                {
                    return "Undefine";
                }

            }
            else
            {
                return "Undefine";
            }
        }

        public static string GetEmail()
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                FormsIdentity ident = HttpContext.Current.User.Identity as FormsIdentity;

                if (ident != null)
                {

                    FormsAuthenticationTicket ticket = ident.Ticket;

                    string[] userData = ticket.UserData.Split('|');

                    if (userData.Length >= 3)
                    {
                        return userData[1];
                    }
                    else
                    {
                        return "Undefine";
                    }

                }
                else
                {
                    return "Undefine";
                }
            }
            else
            {
                return "Undefine";
            }
        }

        public static string GetFullName()
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                FormsIdentity ident = HttpContext.Current.User.Identity as FormsIdentity;

                if (ident != null)
                {

                    FormsAuthenticationTicket ticket = ident.Ticket;

                    string[] userData = ticket.UserData.Split('|');

                    if (userData.Length >= 3)
                    {
                        return userData[2];
                    }
                    else
                    {
                        return "Undefine";
                    }

                }
                else
                {
                    return "Undefine";
                }
            }
            else
            {
                return "Undefine";
            }
        }


        internal static void AddFacebookMetaData(System.Web.UI.Page Page, string type, string url, string title, string image, string description)
        {
            HtmlMeta ogType = new HtmlMeta();
            ogType.Attributes["property"] = "og:type";
            ogType.Content = type;
            Page.Header.Controls.Add(ogType);

            HtmlMeta ogUrl = new HtmlMeta();
            ogUrl.Attributes["property"] = "og:url";
            ogUrl.Content = url;
            Page.Header.Controls.Add(ogUrl);

            HtmlMeta ogTitle = new HtmlMeta();
            ogTitle.Attributes["property"] = "og:title";
            ogTitle.Content = title;
            Page.Header.Controls.Add(ogTitle);

            HtmlMeta ogImage = new HtmlMeta();
            ogImage.Attributes["property"] = "og:image";
            ogImage.Content = image;
            Page.Header.Controls.Add(ogImage);


            HtmlMeta ogDesc = new HtmlMeta();
            ogDesc.Attributes["property"] = "og:description";
            ogDesc.Content = description;
            Page.Header.Controls.Add(ogDesc);
        }

        internal static void SetCanonicalLink(System.Web.UI.Page Page, string url)
        {
            HtmlLink canonicalLink = new HtmlLink();
            canonicalLink.Href = url;
            canonicalLink.Attributes["rel"] = "canonical";
            Page.Header.Controls.Add(canonicalLink);
        }
    }
}